<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
/**
 * Usuario Model
 *
 * @property Foto $Foto
 * @property Hora $Hora
 * @property Venta $Venta
 * @property Alergia $Alergia
 */
class Usuario extends AppModel {


	//The Associations below have been created with all possible keys, those that are not needed can be removed

	public $displayField = "username";

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Foto' => array(
			'className' => 'Foto',
			'foreignKey' => 'usuario_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Hora' => array(
			'className' => 'Hora',
			'foreignKey' => 'usuario_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Venta' => array(
			'className' => 'Venta',
			'foreignKey' => 'usuario_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Alergia' => array(
			'className' => 'Alergia',
			'joinTable' => 'alergias_usuarios',
			'foreignKey' => 'usuario_id',
			'associationForeignKey' => 'alergia_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
		)
	);


	public $validate = array(
		'username' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'El nombre es requerido.'
			)
		),
		'apellido' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'El apellido es requerido.'
			)
		),
		/*'correo' => array(
			'isUnique' => array(
	        		'rule' => 'isUnique',
	        		'require' => true,
	        		'message' => 'Ya existe un correo similar en la base de datos.'
	       	),
	        'notEmpty' => array(
	        		'rule' => 'notEmpty',
	        		'require' => true,
	        		'message' => 'Este campo no puede quedar vacio.'
	        )
		),*/
		'celular' => array(
			'isUnique' => array(
	        		'rule' => 'isUnique',
	        		'require' => true,
	        		'message' => 'Ya se ha registrado este numero anteriormente.'
	       	),
	        'notEmpty' => array(
	        		'rule' => 'notEmpty',
	        		'require' => true,
	        		'message' => 'Este campo no puede quedar vacio.'
	        )
		),
		'password' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'Por favor, asigne una contraseña.'
			)
		),
		'role' => array(
			'valid' => array(
				'rule' => array('inList', array('admin', 'cliente')),
				'message' => 'Por favor ingrese un rol autorizado.',
				'allowEmpty' => false
			)
		)
	);


	public function beforeSave($options = array()) {
		if (isset($this->data[$this->alias]['password'])) {
			$passwordHasher = new BlowfishPasswordHasher();
				$this->data[$this->alias]['password'] = $passwordHasher->hash(
				$this->data[$this->alias]['password']
			);
		}
		return true;
	}

}
